package com.xishu.controller;

import com.xishu.annotation.valid.MethodValidator;
import com.xishu.entity.eft.EftConfig;
import com.xishu.entity.shop.Shop;
import com.xishu.response.ResponseData;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.ShopService;
import com.xishu.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(description = "eft支付相关")
@RestController
public class EftPayController implements Tools {

    private static Logger logger = LoggerFactory.getLogger(EftPayController.class);

    private CommonService commonService = CommonService.getInstance();


    @ApiOperation(value = "新建eft支付配置")
    @PostMapping(value = ("/create/eftConfig"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true, checkShopId = true)
    public ResponseData createEftConfig(@RequestBody EftConfig eftConfig) throws Exception {
        Long shopId = eftConfig.getShopId();
        VerifyUtil.verify(() -> shopId != null);
        VerifyUtil.verify(() -> isNotEmpty(eftConfig.getIpAdress()));
        logger.info("will create eftConfig {}", eftConfig.getTerminalName());

        return commonService.createObject(eftConfig, (config) -> {
            try {
                Shop shop = ShopService.getInstance().findShopById(shopId);
                shop.setEftComplete(true);
                commonService.save(shop);
            } catch (Exception e) {
                logger.error("e", e);
            }
        });
    }


    @ApiOperation(value = "查询eft支付配置")
    @PutMapping(value = ("/find/eftConfig"), produces = ("application/json;charset=UTF-8"))
    public ResponseData findEftConfig(@RequestBody EftConfig eftConfig) throws Exception {
        return commonService.searchResponse(eftConfig);
    }


    @ApiOperation(value = "修改eft支付配置")
    @PutMapping(value = ("/modify/eftConfig"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShopId = true)
    public ResponseData modifyEftConfig(@RequestBody EftConfig eftConfig) throws Exception {
        logger.info("modify the eftConfig of shop {}", eftConfig.getShopId());
        return commonService.updateObject(eftConfig);
    }


    @ApiOperation(value = "删除eft支付配置")
    @DeleteMapping(value = ("/delete/eftConfig"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShopId = true)
    public ResponseData deleteEftConfig(@RequestBody EftConfig eftConfig) throws Exception {
        logger.info("will delete eftConfig {}", eftConfig.getTerminalName());
        EftConfig eftConfigInDb = commonService.findEntity(eftConfig);
        Long shopId = eftConfigInDb.getShopId();
        ResponseData responseData = commonService.delete(eftConfig);

        Shop shop = ShopService.getInstance().findShopById(shopId);
        List<EftConfig> eftConfigList = commonService.searchAll(new EftConfig(shopId));
        if (isEmpty(eftConfigList)) {
            shop.setEftComplete(false);
            commonService.save(shop);
        }

        return responseData;
    }


}
